package servlets;

import bean.User;
import dao.Userdao;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.regex.Pattern;


@WebServlet("/register")
public class register extends HttpServlet {
    private String pattern = "^[A-Za-z0-9\\u4e00-\\u9fa5]+@[a-zA-Z0-9_-]+(\\.[a-zA-Z0-9_-]+)+$";
    private static final long serialVersionUID = 1L;
    private String error;
    private String user_name;
    private String user_password;
    private String tpassword;
    private String email;
    public register() {
        super();
    }
    private boolean is_empty(String info) {
        if(info==null || info.trim().length()<=0) {
            return true;
        }
        return false;
    }
    private boolean judge_info() {
        if(is_empty(user_name)) {
            error = "用户名不能为空！";
            return false;
        }
        if(is_empty(user_password)) {
            error = "密码不能为空！";
            return false;
        }
        if(is_empty(email)) {
            error = "邮箱不能为空！";
            return false;
        }
        if(user_password.equals(tpassword) == false) {
            error = "两次密码不一致！";
            return false;
        }
        if(!Pattern.matches(pattern, email)) {
            error = "邮箱格式不匹配！";
            return false;
        }
        return true;
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
        request.setCharacterEncoding("utf-8");
        response.setContentType("text/html;charset=utf-8");

        user_name = request.getParameter("username");
        user_password = request.getParameter("pw");
        tpassword = request.getParameter("tpw");
        email = request.getParameter("email");
        String kind = "1";

        if(!judge_info()) {
            request.setAttribute("error", error);
            request.getRequestDispatcher("register.jsp").forward(request, response);
        }
        else {
            error = null;
            User user = new User();
            user.setUser_name(user_name);
            user.setUser_password(user_password);
            user.setEmail(email);
            user.setKind(kind);
            Userdao userdao = new Userdao();
            if(userdao.addUser(user)) {
                RequestDispatcher forword = request.getRequestDispatcher("login.jsp");
                forword.forward(request, response);
            }
            else {
                error = "用户名已存在！";
                request.setAttribute("error", error);
                request.getRequestDispatcher("register.jsp").forward(request, response);
            }
        }
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}
